package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.BedMapper;
import cn.tedu.carehome.foundation.mapper.UpCheckInfoMapper;
import cn.tedu.carehome.foundation.pojo.dto.TellCheckInfoUpdateDTO;
import cn.tedu.carehome.foundation.pojo.dto.UpCheckInfoAddNewDto;
import cn.tedu.carehome.foundation.pojo.dto.UpCheckInfoUpdateDTO;
import cn.tedu.carehome.foundation.pojo.entity.UpCheckInfo;
import cn.tedu.carehome.foundation.pojo.vo.UpCheckInfoAllOlderAddInfoVo;
import cn.tedu.carehome.foundation.pojo.vo.UpCheckInfoListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.UpCheckInfoStandardVo;
import cn.tedu.carehome.foundation.service.IUpCheckInfoService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UpCheckInfoServiceImpl implements IUpCheckInfoService {
    @Autowired
    UpCheckInfoMapper upcheckInfoMapper;

    @Autowired
    BedMapper bedMapper;

    @Override
    public void deleteByid(Long id) {
//        log.debug("开始处理【删除入住信息】列表业务！");
//        if(tellcheckInfoMapper.getStandarByid(id)==null)
//        {
//            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除错误,非法此id!");
//        }
//        if(bedMapper.countByCheckId(id)>0)
//        {
//            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败,改老人正在入住,请先退出该老人的床位！");
//        }
        upcheckInfoMapper.deleteByid(id);
        log.debug("删除成功！");
    }

    @Override
    public void deleteByids(Long[] ids) {
        log.debug("开始处理【批量删除入住信息】列表业务！");
        if(bedMapper.countByCheckIds(ids)>0)
        {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败,其中有老人正在入住,请先退出该老人的床位！");
        }
        upcheckInfoMapper.deleteBatchByids(ids);
    }

    @Override
    public List<UpCheckInfoListItemVO> list(String Content) {
        log.debug("开始处理查询【查询入住信息全部】业务");
        return upcheckInfoMapper.list(Content);
    }

    @Override
    public List<UpCheckInfoListItemVO> listLimit(int page,int size,String Content) {
        log.debug("开始处理【查询入住登记】列表业务！");
        return upcheckInfoMapper.listLimit(page,size,Content);
    }

    @Override
    public List<UpCheckInfoAllOlderAddInfoVo> listAllOlderInfo(String Content)
    {
        return upcheckInfoMapper.listAllOlderInfo(Content);
    }

    @Override
    public UpCheckInfoStandardVo checkByid(Long id) {
       return upcheckInfoMapper.getStandarByid(id);
    }

    @Override
    public int count(String Content) {
        return upcheckInfoMapper.count(Content);
    }

    @Override
    public void addNew(UpCheckInfoAddNewDto upcheckInfoAddNewDto) {
//        if(tellcheckInfoMapper.countByOlderId(tellcheckInfoAddNewDto.getName())>0)
//        {
//            throw new ServiceException(ServiceCode.ERR_CONFLICT,"该老人入住登记信息已存在！");
//        }
        UpCheckInfo  checkInfo = new UpCheckInfo();
//        checkInfo.setConsultationdate(checkInfo.getConsultationdate().substring(0,10));
        BeanUtils.copyProperties(upcheckInfoAddNewDto,checkInfo);
        checkInfo.setConsultationdate(checkInfo.getConsultationdate().substring(0,10));
        upcheckInfoMapper.insert(checkInfo);
    }

    @Override
    public void update(UpCheckInfoUpdateDTO upcheckInfoUpdateDTO) {
//        if(tellcheckInfoMapper.countByOlderIdNotId(tellcheckInfoUpdateDTO.getId(),checkInfoUpdateDTO.getOlderId())>0)
//        {
//            throw new ServiceException(ServiceCode.ERR_CONFLICT,"修改失败,该老人入住登记信息已存在!");
//        }
        upcheckInfoMapper.update(upcheckInfoUpdateDTO);
    }


}
